/**
 * @file
 * A JavaScript file for the theme.
 *
 * In order for this JavaScript to be loaded on pages, see the instructions in
 * the README.txt next to this file.
 */

// JavaScript should be made compatible with libraries other than jQuery by
// wrapping it with an "anonymous closure". See:
// - http://drupal.org/node/1446420
// - http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
var PREVI_COLUMNES=3;
var projecte_id=0;
var casella_id=0;

(function ($, Drupal, window, document, undefined) {

	var desti="";

// Place your code here.
	$(function(){
		if ($(".front")[0] && !Drupal.settings.qubba.mobile_device){
			//$("#main-menu").hide();
			$("#logo-home").hide();
			$("#logo-caixa").show();
		    $('#home-div a').attr("href","#");
			
	        $('#logo-caixa').click(function(e){
			
	    	    $('#home-div')
	    	    .sprite({
	    	        fps: 6, 
	    	        no_of_frames: 14,
	    	        start_at_frame: 0,
	    			rewind: false,
	    			play_frames:14,
	    	        on_first_frame: function(obj) {
	    	            if (window.console) {
	    	                console.log('first frame');
	    	            }
	    	        },
	    	        on_last_frame: function(obj) {
	    	            // you could stop the sprite here with, e.g.
	    	             //obj.spStop();
	    	            if (window.console) {
	    	                console.log('last frame');
	    	            }
	    	        },
	    	        on_frame: {
	    	            1: function(obj) {$("#logo-caixa").hide();},
	    	            	12: function(obj) {
	    	                // you could change the 'state' of the
	    	                // sprite here with, e.g. obj.spState(2);
	    	        		$("#main-menu").show();
	    	    			$("#logo-home").show();
	    	        		if (window.console) {
	    	                    console.log('frame 2');
	    	                }
	    	            }
	    	        }
	    	    });		
	        });

	        $('#main-menu a').click(function(e){
	        	e.preventDefault();
	        	$('#home-div').unbind();
	        	desti=$(this).attr("href");
	        	tanca_caixa();
	        	return(false);
	        });    			
		}

		
		/***** LLISTAT PROJECTES, TIRETA **/
		$(".field-name-title-field",".page-taxonomy-term").each(function(){
			/*** TEST ***
			
			
			switch(QueryString.p){
			case '1':
				casella_id=projecte_id % PREVI_COLUMNES; //columnes consecutives
				break;
			case '2':
				casella_id=Math.floor((Math.random()*PREVI_COLUMNES)); //columna random
				break;
			default:
				casella_id=0; // sempre columna 0
				break;
			}
			/** FI TEST **/
			casella_id=projecte_id % PREVI_COLUMNES; //columnes consecutives

			
			if ($(this).next().length) {
				$(this).addClass("previ previ-link-"+projecte_id);
				$(this).find("a").addClass("previ-link-"+projecte_id);
				$(this).find("a").attr("previ",projecte_id);
				$(this).find("a").attr("casella",casella_id);
			}
			else{				
				$(this).addClass("previ previ-link-"+projecte_id);
				$(this).find("a").addClass("previ-link-"+projecte_id);
				//$(this).addClass("no-previ previ-link-"+projecte_id);
				//$(this).find("a").removeAttr('href');
			}
			projecte_id++;
		});
		
		$(".field-name-title-field.previ a",".page-taxonomy-term").hover(previ_on,previ_off);
});
function previ_on(e){

	casella_id=$(this).attr("casella");
	$(this).addClass("previ-projecte-hover");
	$("#projecte-previ-"+casella_id).html($(this).parents(".previ").next().html());
	$("#projecte-previ-"+casella_id+" img").hide();
	$("#projecte-previ-"+casella_id+" img").fadeIn(500);
}					
function previ_off(e){
	casella_id=$(this).attr("casella")
	$(this).removeClass("previ-projecte-hover");
	$("#projecte-previ-"+casella_id+" img").fadeOut(200);
}			


/* ANIMACIO TANCA */
function tanca_caixa(){
	$("#main-menu").hide();
	$("#logo-home").hide();
	$('#home-div').destroy();
    $('#home-div')
    .sprite({
        fps: 18, 
        no_of_frames: 14,
        start_at_frame: 14,
		rewind: true,
		play_frames:13,	
        on_frame: {
            1: function(obj) {
            	document.location=desti;
        		if (window.console) {
                    console.log('frame xxx');
                }
            }
        }
     });           
}
})(jQuery, Drupal, this, this.document);


var QueryString = function () {
	  // This function is anonymous, is executed immediately and 
	  // the return value is assigned to QueryString!
	  var query_string = {};
	  var query = window.location.search.substring(1);
	  var vars = query.split("&");
	  for (var i=0;i<vars.length;i++) {
	    var pair = vars[i].split("=");
	    	// If first entry with this name
	    if (typeof query_string[pair[0]] === "undefined") {
	      query_string[pair[0]] = pair[1];
	    	// If second entry with this name
	    } else if (typeof query_string[pair[0]] === "string") {
	      var arr = [ query_string[pair[0]], pair[1] ];
	      query_string[pair[0]] = arr;
	    	// If third or later entry with this name
	    } else {
	      query_string[pair[0]].push(pair[1]);
	    }
	  } 
	    return query_string;
	} ();